package com.example.demo.controller;

import com.example.demo.domain.UserInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    public static final Map<String, UserInfo> localMap = new HashMap<>();

    @GetMapping("/info/{id}")
    public ResponseEntity<UserInfo> getUser(@PathVariable String id, HttpServletRequest request) {
        UserInfo userInfo = localMap.get(id);
        if (userInfo == null) {
            return ResponseEntity.ok(new UserInfo());
        }
        System.out.println("user获取的用户信息: " + userInfo.toString());
        String token = request.getHeader("token");
        System.out.println(token);
        return ResponseEntity.ok(userInfo);
    }

    @PostMapping("/update")
    public ResponseEntity<String> updateUser(@RequestBody UserInfo user){
        localMap.put(user.getId(), user);
        System.out.println("user更新的用户信息: " + user.toString());
        return ResponseEntity.ok("更新成功");
    }
}
